package com.timmy.bluetooth.connect;

/**
 * 蓝牙名字
 */
public class Command implements  ICommand {

    static final String TAG = Command.class.getSimpleName();
    public static byte NC = 0;
    public static byte SET = 1;

    public enum CommandType {
        BASE_ACTION,            //0 - 100		振动频率
        CONTROL_STYLE,            //1 - 5			//震动方式
        MOTOR_STATUS,
        SHOCK_FUNCION,               //0  -  2		改变左右胸
        SLEEP,
        MAC
    }

    private static Command instance;

    public static Command getInstance() {
        if (instance == null) {
            instance = new Command();
        }
        return instance;
    }

    static final int INDEX_0_HEAD = 0;
    // checksum
    static final int INDEX_1_CHECKSUM = 1;
    // base actions
    static final int INDEX_2_BASE_ACTION = 2;
    //control instruction.
    static final int INDEX_3_CONTROL_WORD = 3;
    //motor shock function
    static final int INDEX_4_SHOCK = 4;
    //tail code
    static final int INDEX_5_TAIL = 5;

    // ͷβ�ֽ�
    private static final byte HEAD_CODE = (byte) 0xBB;
    private static final byte END_CODE = (byte) 0x82;

    public static final byte BIT_FLAG_7 = (byte) (0x01 << 7);
    public static final byte BIT_FLAG_6 = (byte) (0x01 << 6);
    public static final byte BIT_FLAG_5 = (byte) (0x01 << 5);
    public static final byte BIT_FLAG_4 = (byte) (0x01 << 4);
    public static final byte BIT_FLAG_3 = (byte) (0x01 << 3);
    public static final byte BIT_FLAG_2 = (byte) (0x01 << 2);
    public static final byte BIT_FLAG_1 = (byte) (0x01 << 1);
    public static final byte BIT_FLAG_0 = (byte) 0x01;

    /**
     * command cache
     */
    private byte[] data = new byte[6];


    public Command() {
        reset();
    }

    public synchronized void reset() {
        //
        data[INDEX_0_HEAD] = HEAD_CODE;         //固定的
        data[INDEX_2_BASE_ACTION] = 0x00;
        data[INDEX_3_CONTROL_WORD] = 0x01;
        data[INDEX_4_SHOCK] = 0x00;
        data[INDEX_5_TAIL] = END_CODE;          //固定

        buildValidateCode();
    }

    // 设置校对值
    private synchronized void buildValidateCode() {
        byte validate = (byte) ((data[2] + data[3] + data[4] + data[5]) & 0xff);

        data[INDEX_1_CHECKSUM] = (byte) validate;
    }

    /**
     * 向蓝牙设备发送数据
     */
    public void buildCommandByType(CommandType commandType, byte value) {
        buildCommand(commandType, value);
    }


    /**
     * 根据指令类型去设置16进制数
     *
     */
    private synchronized Command buildCommand(CommandType commandType,
                                              byte value) {

        switch (commandType) {
            case BASE_ACTION:
                data[INDEX_2_BASE_ACTION] = (byte) (value & 0x0f);
                break;
            case CONTROL_STYLE:
                data[INDEX_3_CONTROL_WORD] &= ~(BIT_FLAG_0 | BIT_FLAG_1);
                data[INDEX_3_CONTROL_WORD] |= (value & (BIT_FLAG_0 | BIT_FLAG_1));
                break;
            case MOTOR_STATUS:
                if (0 == value)
                    data[INDEX_3_CONTROL_WORD] &= ~BIT_FLAG_3;
                else
                    data[INDEX_3_CONTROL_WORD] |= BIT_FLAG_3;
                break;
            case SLEEP:
                if (0 == value)
                    data[INDEX_3_CONTROL_WORD] &= ~BIT_FLAG_2;
                else
                    data[INDEX_3_CONTROL_WORD] |= BIT_FLAG_2;
                break;
            case MAC:
                if (0 == value)
                    data[INDEX_3_CONTROL_WORD] &= ~BIT_FLAG_4;
                else
                    data[INDEX_3_CONTROL_WORD] |= BIT_FLAG_4;
                break;
            case SHOCK_FUNCION:
                if (value > 0)
                    data[INDEX_4_SHOCK] = (byte) (value | BIT_FLAG_7);        //0-10
                else
                    data[INDEX_4_SHOCK] = value;
        }

        buildValidateCode();

        return this;
    }

    /**
     *
     * 返回的是向蓝牙发送的数组
     */
    public synchronized byte[] getData() {
        return data;
    }
}
